﻿using System;
using System.Net;
using Enyim.Caching;
using System.Web.Mvc;
using SilenthinkService;
using SilenthinkContract;
using SilenthinkContract.Domain;

namespace Web.Manage.Controllers
{
    public class HelpController : BaseController
    {
        public HelpController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient) : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(string title, byte? category, bool? showOnMenu, int index = 1, int size = 20)
        {
            ViewBag.Title = title;
            ViewBag.Category = category;
            ViewBag.ShowOnMenu = showOnMenu;
            var model = BaseService.GetPagedHelpQuestion(index, size, title, category, showOnMenu).ToPagedList(index, size);
            return View(model);
        }

        public ActionResult Question(int? param)
        {
            var model = new HelpQuestion();
            if (param.HasValue && param.Value > 0)
            {
                model = BaseService.GetHelpQuestion(param.Value);
            }
            return View(model);
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult Question(int? param, HelpQuestion model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;
            if (param.HasValue && param.Value > 0)
            {
                var old = BaseService.GetHelpQuestion(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = BaseService.UpdateHelpQuestion(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/Help") : Json(HttpStatusCode.BadRequest, "更新失败");
                }
            }
            else
            {
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = BaseService.CreateHelpQuestion(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", true, "/Help");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        [HttpPost]
        public ActionResult Delete(int? param)
        {
            if (param.HasValue && param.Value > 0)
            {
                var result = BaseService.DeleteHelpQuestion(param.Value);
                return result ? Json(HttpStatusCode.OK, "删除成功", false, "/Help") : Json(HttpStatusCode.BadRequest, "删除失败");

            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

    }
}
